package com.zc.webnews.ui;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.zc.webnews.R;
import com.zc.webnews.adapter.CollectionAdapter;
import com.zc.webnews.adapter.NewsItemAdapter;
import com.zc.webnews.application.Constant;
import com.zc.webnews.bean.NewsDetailsBean;
import com.zc.webnews.utils.MyBmobUtils;
import com.zc.webnews.view.WhorlView;

import java.util.ArrayList;
import java.util.List;

public class ShowCollectionActivity extends BaseTranslucentActivity implements AdapterView.OnItemClickListener {

    private ListView listView;
    private Toolbar toolbar;
    private ImageView back;
    private TextView title;
    private WhorlView whorlView;
    private TextView tips;
    private CollectionAdapter adapter;
    //收藏的集合
    public static List<NewsDetailsBean> collectionList = new ArrayList<>();
    public Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what){
                case Constant.SUCCESS:
                    if (collectionList.size()==0){
                        whorlView.setVisibility(View.GONE);
                        tips.setVisibility(View.VISIBLE);
                        listView.setVisibility(View.GONE);
                    }else {
                        tips.setVisibility(View.GONE);
                        whorlView.setVisibility(View.GONE);
                        listView.setVisibility(View.VISIBLE);
                        adapter.notifyDataSetChanged();
                    }
                    whorlView.stop();
                    break;
            }
        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_show_collect);
        initView();
        initData();
        initListener();
    }

    private void initListener() {
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        listView.setOnItemClickListener(this);
    }


    private void initData() {
        tips.setVisibility(View.GONE);
        whorlView.setVisibility(View.VISIBLE);
        listView.setVisibility(View.GONE);
        whorlView.start();
         /*获取收藏列表*/
        new Thread(new Runnable() {
            @Override
            public void run() {
                MyBmobUtils myBombUtils = new MyBmobUtils();
                myBombUtils.context = ShowCollectionActivity.this;
                myBombUtils.queryCollection();
            }
        }).start();
        title.setText(R.string.shoucang);
        adapter = new CollectionAdapter(this,collectionList);
        listView.setAdapter(adapter);
    }

    private void initView() {
        toolbar = (Toolbar) findViewById(R.id.show_collect_title);
        back = (ImageView) toolbar.findViewById(R.id.iv_back_titlebar);
        title = (TextView) toolbar.findViewById(R.id.tv_title_bar);
        listView = (ListView) findViewById(R.id.show_collect_listView);
        whorlView = (WhorlView) findViewById(R.id.collect_loading);
        tips = (TextView) findViewById(R.id.collect_tips);
        setOrChangeTranslucentColor(toolbar,getResources().getColor(R.color.dip_red));
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, final int position, long id) {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setItems(new String[]{"查看新闻详情", "删除收藏"}, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                switch (which){
                    case 0:
                        Intent intent = new Intent(ShowCollectionActivity.this,ShowNewsActivity.class);
                        intent.putExtra("url",collectionList.get(position).getUrl());
                        startActivity(intent);
                        break;
                    case 1:
                        MyBmobUtils myBmobUtils = new MyBmobUtils();
                        myBmobUtils.context = ShowCollectionActivity.this;
                        myBmobUtils.deleteCollection(collectionList.get(position).getObjectId());
                        collectionList.remove(position);
                        adapter.notifyDataSetChanged();
                        break;
                }
            }
        });
        builder.show();
    }
}
